package com.appspot.mypartymanager.server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appspot.mypartymanager.entity.party.Party;
import com.appspot.mypartymanager.exception.InvalidPartyException;
import com.appspot.mypartymanager.service.PartyService;
import com.appspot.mypartymanager.service.impl.PartyServiceImpl;

public class TestServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		// EVENT TEST
		
		PartyService es = new PartyServiceImpl();
		
		// definir festas
		Date dateCreated = new Date();
		String ownerGoogleAcount = "thefordexter@gmail.com";
		String name = "Casamento de Priscilla e Carlos";
		String announcement = "Venha comemorar o casamento de Priscilla e Carlos";
		Date partyDate = new Date(20100409L);
		List<String> partyManagersGoogleAcounts = new ArrayList<String>();
		String googleMapsAdress = "http://maps.google.com.br/maps?f=q&source=s_q&hl=pt-BR&geocode=&q=Rua+Aiera,+652+-+Vila+da+Penha+-+Rio+de+Janeiro+-+RJ&sll=-22.854585,-43.304973&sspn=0.032309,0.038581&ie=UTF8&hq=&hnear=R.+Aiera,+652+-+Vila+Cosmos,+Rio+de+Janeiro,+21220-020&z=17&iwloc=A";		
		String adress = "Rua Aiera, 652 - Vila da Penha - Rio de Janeiro - RJ";
		Party party1 = new Party(dateCreated, ownerGoogleAcount, name, announcement, partyDate, partyManagersGoogleAcounts, googleMapsAdress, adress);
		try {
			es.createParty(party1);
		} catch (InvalidPartyException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
				
		dateCreated = new Date();
		ownerGoogleAcount = "thefordexter@gmail.com";
		name = "Festa de Aniversário - 28 anos";
		announcement = "Venha comemorar meu 28o. ano de vida!!!";
		partyDate = new Date(20110522L);
		partyManagersGoogleAcounts = null;
		googleMapsAdress = null;		
		adress = null;
		Party party2 = new Party(dateCreated, ownerGoogleAcount, name, announcement, partyDate, partyManagersGoogleAcounts, googleMapsAdress, adress);
		try {
			es.createParty(party2);
		} catch (InvalidPartyException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		dateCreated = new Date();
		ownerGoogleAcount = "carlosaugustoleao@gmail.com";
		name = "teste party name 1";
		announcement = "bla bla bla";
		partyDate = new Date(20100409L);
		partyManagersGoogleAcounts = null;
		googleMapsAdress = null;		
		adress = null;
		Party party3 = new Party(dateCreated, ownerGoogleAcount, name, announcement, partyDate, partyManagersGoogleAcounts, googleMapsAdress, adress);
		try {
			es.createParty(party3);
		} catch (InvalidPartyException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		Long idPartyToDelete = null;
		
		// consultar partyos do usuario
		String user = "thefordexter@gmail.com";
		List<Party> userPartys = es.getPartysByUser(user);
		
		System.out.println("User " + user + " partys:");
		for (int i = 0; i < userPartys.size(); i++) {
			System.out.println(userPartys.get(i));
			idPartyToDelete = userPartys.get(i).getId();
		}
	
		// testar atualização do partyo, antes de apagá-lo
		Party partyToUpdate = es.getPartyById(idPartyToDelete);
		partyToUpdate.setName("Nome da Festa Atualizado");
		try {
			es.updateParty(partyToUpdate);
		} catch (InvalidPartyException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		try {
			es.updateParty(partyToUpdate);
		} catch (InvalidPartyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
		// excluir a ultima festa da lista para teste
		System.out.println("Excluindo Festa " + es.getPartyById(idPartyToDelete));
		es.deleteParty(idPartyToDelete);
		System.out.println("Festa excluida");
		
		
			
	}

}
